<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"  media="all">
    <script src="${pageContext.request.contextPath}/layui/layui.js" charset="utf-8"></script>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top:5px;">
    <legend>商品类型信息总览</legend>
</fieldset>

<div class="layui-tab layui-tab-card">
    <ul class="layui-tab-title">
        <li class="layui-this">键盘颜色</li>
        <li>轴体类型</li>
        <li>背光颜色</li>
    </ul>
    <div class="layui-tab-content" style="height:500px">
        <div class="layui-tab-item layui-show">
            <form style="display: none;" class="layui-form" lay-filter="keyboardcolorForm" id="keyboardcolorForm">
                <input type="hidden" name="keyboardcolorid">
                <div class="layui-form-item">
                    <label class="layui-form-label">键盘颜色</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keyboardcolorname" placeholder="请输入键盘颜色" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit lay-filter="formDemo1">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
            <%--搜索--%>
            <div class="demoTable">
                <div class="layui-inline">
                    <input class="layui-input" placeholder="请输入ID搜索" name="keyboardcolorid" id="keyboardcolorid" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <input class="layui-input" placeholder="请输入键盘颜色搜索" name="keyboardcolorname" id="keyboardcolorname" autocomplete="off">
                </div>

                <button class="layui-btn"  id="keyboardcolorSearch" data-type="reload">搜索</button>
            </div>
            <table id="demo1" lay-filter="test1"></table>
        </div>

        <div class="layui-tab-item">
            <form style="display: none;" class="layui-form" lay-filter="ShaftchooseForm" id="ShaftchooseForm">
                <input type="hidden" name="shaftchooseid">
                <div class="layui-form-item" style="margin-top: 15px">
                    <label class="layui-form-label">键轴</label>
                    <div class="layui-input-inline">
                        <input type="text" name="shaftchoose" placeholder="请输入键轴类型" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item" style="margin-left: 100px">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit lay-filter="formDemo2">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
            <table id="demo2" lay-filter="test2"></table>

        </div>

        <div class="layui-tab-item">
            <form style="display: none;" class="layui-form" lay-filter="backlightcolorForm" id="backlightcolorForm">
                <input type="hidden" name="backlightcolorid">
                <div class="layui-form-item">
                    <label class="layui-form-label">背光颜色</label>
                    <div class="layui-input-inline">
                        <input type="text" name="backlightcolorname" placeholder="请输入背光颜色" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit lay-filter="formDemo3">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>

            <table id="demo3" lay-filter="test3"></table>
        </div>
    </div>
</div>
<%--键盘颜色头部工具栏--%>
<script type="text/html" id="toolbarDemo1">
    <div class="layui-btn-container">
        {{#  if(${sessionScope.staff.branchid} == 12138){ }}
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        {{#  } else { }}
        {{#  } }}
    </div>
</script>

<%--键盘颜色右侧工具栏--%>
<script type="text/html" id="barDemo1">
    {{#  if(${sessionScope.staff.branchid} == 12138){ }}
    <a class="layui-btn layui-btn-xs" lay-event="updateKeyboard">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } else { }}
    {{#  } }}
</script>

<%--轴体类型头部工具栏--%>
<script type="text/html" id="toolbarDemo2">
    <div class="layui-btn-container">
        {{#  if(${sessionScope.staff.branchid} == 12138){ }}
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        {{#  } else { }}
        {{#  } }}
    </div>
</script>

<%--轴体类型右侧工具栏--%>
<script type="text/html" id="barDemo2">
    {{#  if(${sessionScope.staff.branchid} == 12138){ }}
    <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } else { }}
    {{#  } }}
</script>

<%--背光颜色头部工具栏--%>
<script type="text/html" id="toolbarDemo3">
    <div class="layui-btn-container">
        {{#  if(${sessionScope.staff.branchid} == 12138){ }}
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        {{#  } else { }}
        {{#  } }}
    </div>
</script>

<%--背光颜色右侧工具栏--%>
<script type="text/html" id="barDemo3">
    {{#  if(${sessionScope.staff.branchid} == 12138){ }}
    <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } else { }}
    {{#  } }}
</script>
<script>
    layui.use(['element','table','form','jquery','layer'], function(){
        var $ = layui.jquery;
        var element = layui.element;//Tab的切换功能，切换事件监听等，需要依赖element模块
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        /*===================================================键盘颜色=========================================*/
//表单的提交事件
        //监听事件
        form.on('submit(formDemo1)', function(data){
            //获取表单区域所有值
            var keyboardData = form.val("keyboardcolorForm");
            console.log(keyboardData);
            //向后台发送请求
            $.ajax({
                url:'${pageContext.request.contextPath}/keyboardcolor/addOrUpdateKeyboardcolor',
                type:"POST",
                data:keyboardData,
                //因为layui 在表单提交时 有时候会自动拦截我们的异步请求
                //所以此处改位同步请求
                async:false,
                dataType:"json",
                success:function (result) {
                    layer.closeAll();//关闭弹框
                    if (result.code==2000) {
                        //重载表格
                        table.reload("test1");
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            })
        });


        //搜索时，执行表格的重载事件
        $("#keyboardcolorSearch").click(function () {
            table.reload('test1', {
                where: { //设定异步数据接口的额外参数，任意设
                    keyboardcolorid:$("#keyboardcolorid").val(),
                    keyboardcolorname:$("#keyboardcolorname").val()
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            }); //只重载数据
        })

        //加载表格的实例
        table.render({
            id:"test1",
            //elem element 元素---》表格的容器
            elem: '#demo1'//table标签的id属性值
            //,height: 312//整个表格的高度
            ,url: '${pageContext.request.contextPath}/keyboardcolor/listByCondition' //数据接口
            ,toolbar: '#toolbarDemo1' //开启头部工具栏，并为其绑定左侧模板
            ,title: '键盘颜色表'
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'keyboardcolorid', title: 'ID'}
                ,{field: 'keyboardcolorname', title: '键盘颜色'}
                ,{fixed: 'right', title:'操作',width:200, toolbar: '#barDemo1'}
            ]],
            page: true,//开启分页
            limit:10,//设置每页条数
            limits:[2,5,10,15,20]
        });

        //头工具栏事件
        //toolbar()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('toolbar(test1)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    //先执行清空表单的方法，再打开弹出框
                    $("#keyboardcolorForm")[0].reset();
                    layer.open({
                        type: 1,
                        title:'新增键盘颜色',
                        content: $("#keyboardcolorForm"), //这里content是一个普通的String
                        area: '380px',
                        offset: '100px'
                    });
                    break;
            };
        });

        //监听行工具事件
        ////tool()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('tool(test1)', function(obj){
            //取出当前点击的行的数据
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除吗', function(index){
                    $.ajax({
                        type:'get',
                        url:'${pageContext.request.contextPath}/keyboardcolor/deleteKeyboardcolor',
                        data:{'keyboardcolorid':data.keyboardcolorid},
                        dataType: 'json',
                        success:function (result){
                            if (result.code == 1) {
                                layer.msg(result.msg)
                            }else {
                                layer.msg(result.msg)
                            }
                            table.reload('test1')
                        }
                    })
                    layer.close(index);
                });
            }else if(obj.event === 'updateKeyboard'){
                //先执行清空表单的方法，再打开弹出框
                $("#keyboardcolorForm")[0].reset();
                layer.open({
                    type:1,
                    content: $("#keyboardcolorForm"), //这里content是一个普通的String
                    area: '380px',
                    offset: 't'
                });
                //填充表单
                form.val('keyboardcolorForm', data);
            }
        });
        /*=========================================轴体类型===================================================*/
        //表单的提交事件
        //监听事件
        form.on('submit(formDemo2)', function(data){
            //获取表单区域所有值
            var backlightcolorData = form.val("ShaftchooseForm");
            //向后台发送请求
            $.ajax({
                url:'${pageContext.request.contextPath}/shaftchoose/addOrUpdateShaftChoose',
                type:"POST",
                data:backlightcolorData,
                //因为layui 在表单提交时 有时候会自动拦截我们的异步请求
                //所以此处改位同步请求
                async:false,
                dataType:"json",
                success:function (result) {
                    layer.closeAll();//关闭弹框
                    if (result.code==2000) {
                        //重载表格
                        table.reload("test2");
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }
                    //重载表格
                    table.reload("test2");
                }
            })
        });


        //加载表格的实例
        table.render({
            id:"test2",
            //elem element 元素---》表格的容器
            elem: '#demo2'//table标签的id属性值
            //,height: 312//整个表格的高度
            ,url: '${pageContext.request.contextPath}/shaftchoose/listByCondition' //数据接口
            ,toolbar: '#toolbarDemo2' //开启头部工具栏，并为其绑定左侧模板
            ,title: '键盘颜色表'
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'shaftchooseid', title: 'ID'}
                ,{field: 'shaftchoose', title: '键轴类型'}
                ,{fixed: 'right', title:'操作',width:200, toolbar: '#barDemo2'}
            ]],
            page: true,//开启分页
            limit:10,//设置每页条数
            limits:[5,10,15,20]
        });
        //头工具栏事件
        //toolbar()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('toolbar(test2)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                /**
                 * case后边的具体字符串跟 toolbarDemo2中的 lay-event属性值一样
                 */
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
                case 'add':
                    //先执行清空表单的方法，再打开弹出框
                    $("#ShaftchooseForm")[0].reset();
                    layer.open({
                        type: 1,
                        title:'新增键轴类型',
                        content: $("#ShaftchooseForm"), //这里content是一个普通的String
                        area: '380px',
                        offset: '100px'
                    });
                    break;
            };
        });

        //监听行工具事件
        ////tool()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('tool(test2)', function(obj){
            //取出当前点击的行的数据
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除吗', function(index){
                    $.ajax({
                        type:'get',
                        url:'${pageContext.request.contextPath}/shaftchoose/deleteShaftChoose',
                        data:{'shaftchooseid':data.shaftchooseid},
                        dataType: 'json',
                        success:function (result){
                            if (result.code == 1) {
                                layer.msg(result.msg)
                            }else {
                                layer.msg(result.msg)
                            }
                            table.reload('test2')
                        }
                    })
                    layer.close(index);
                });
            }else if(obj.event === 'update'){
                //先执行清空表单的方法，再打开弹出框
                $("#ShaftchooseForm")[0].reset();
                layer.open({
                    type:1,
                    title:"修改信息",
                    content: $("#ShaftchooseForm"), //这里content是一个普通的String
                    area: '380px',
                    offset: '100px'
                });
                //填充表单
                form.val('ShaftchooseForm', data);
            }
        });
        /*=========================================背光颜色===================================================*/
        //表单的提交事件
        //监听事件
        form.on('submit(formDemo3)', function(data){
            //获取表单区域所有值
            var backlightcolorData = form.val("backlightcolorForm");
            //向后台发送请求
            $.ajax({
                url:'${pageContext.request.contextPath}/backlightcolor/addOrUpdateBackLightColor',
                type:"POST",
                data:backlightcolorData,
                //因为layui 在表单提交时 有时候会自动拦截我们的异步请求
                //所以此处改位同步请求
                async:false,
                dataType:"json",
                success:function (result) {
                    layer.closeAll();//关闭弹框
                    if (result.code==2000) {

                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }
                    //重载表格
                    table.reload("test3");
                }
            })
        });




        //加载表格的实例
        table.render({
            id:"test3",
            //elem element 元素---》表格的容器
            elem: '#demo3'//table标签的id属性值
            //,height: 312//整个表格的高度
            ,url: '${pageContext.request.contextPath}/backlightcolor/listByCondition' //数据接口
            ,toolbar: '#toolbarDemo3' //开启头部工具栏，并为其绑定左侧模板
            ,title: '键盘颜色表'
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'backlightcolorid', title: 'ID'}
                ,{field: 'backlightcolorname', title: '键盘颜色'}
                ,{fixed: 'right', title:'操作',width:200, toolbar: '#barDemo3'}
            ]],
            page: true,//开启分页
            limit:10,//设置每页条数
            limits:[5,10,15,20]
        });

        //头工具栏事件
        //toolbar()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('toolbar(test3)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                /**
                 * case后边的具体字符串跟 toolbarDemo3中的 lay-event属性值一样
                 */
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
                case 'add':
                    //先执行清空表单的方法，再打开弹出框
                    $("#backlightcolorForm")[0].reset();
                    layer.open({
                        type: 1,
                        title:'新增背光颜色',
                        content: $("#backlightcolorForm"), //这里content是一个普通的String
                        area: '380px',
                        offset: 't'
                    });
                    break;
            };
        });

        //监听行工具事件
        ////tool()括号中的参数就是  table容器中 lay-filter属性的值
        table.on('tool(test3)', function(obj){
            //取出当前点击的行的数据
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除吗', function(index){
                    $.ajax({
                        type:'get',
                        url:'${pageContext.request.contextPath}/backlightcolor/deleteBackLightColor',
                        data:{'backlightcolorid':data.backlightcolorid},
                        dataType: 'json',
                        success:function (result){
                            if (result.code == 1) {
                                layer.msg(result.msg)
                            }else {
                                layer.msg(result.msg)
                            }
                            table.reload('test3')
                        }
                    })
                    layer.close(index);
                });
            }else if(obj.event === 'update'){
                //先执行清空表单的方法，再打开弹出框
                $("#backlightcolorForm")[0].reset();
                layer.open({
                    type:1,
                    content: $("#backlightcolorForm"), //这里content是一个普通的String
                    area: '380px',
                    offset: 't'
                });
                //填充表单
                form.val('backlightcolorForm', data);
            }
        });
        /*==================================================================================================*/
        //触发事件
        var active = {
            tabAdd: function(){
                //新增一个Tab项
                element.tabAdd('demo', {
                    title: '新选项'+ (Math.random()*1000|0) //用于演示
                    ,content: '内容'+ (Math.random()*1000|0)
                    ,id: new Date().getTime() //实际使用一般是规定好的id，这里以时间戳模拟下
                })
            }
            ,tabDelete: function(othis){
                //删除指定Tab项
                element.tabDelete('demo', '44'); //删除：“商品管理”


                othis.addClass('layui-btn-disabled');
            }
            ,tabChange: function(){
                //切换到指定Tab项
                element.tabChange('demo', '22'); //切换到：用户管理
            }
        };

        $('.site-demo-active').on('click', function(){
            var othis = $(this), type = othis.data('type');
            active[type] ? active[type].call(this, othis) : '';
        });

        //Hash地址的定位
        var layid = location.hash.replace(/^#test=/, '');
        element.tabChange('test', layid);

        element.on('tab(test)', function(elem){
            location.hash = 'test='+ $(this).attr('lay-id');
        });

    });
</script>

</body>
</html>